import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'; 

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: {
      title: '登录',
      hideInMenu: true
    }
  },
  
  {
    path: '/',
    component: () => import('../views/Layout/index.vue'),
    redirect:'/login',
    children: [
      // {
      //   // path: 'dashboard',
      //   // name: 'Dashboard',
      //   // component: () => import('../views/Dashboard/index.vue'),
      //   // meta: {
      //   //   title: '首页仪表盘',
      //   //   icon: '🏠'
      //   // }
      // },
      //             // 系统管理
      {
        path: 'system',
        name: 'System',
        meta: {
          title: '系统管理',
          icon: '⚙️'
        },
        children: [
          {
            path: 'user',
            name: 'SystemUser',
            component: () => import('../views/system/user/index.vue'),
            meta: {
              title: '用户管理',
              icon: '👤'
            }
          }
    
        ]
      },
      

      // 档案管理
      {
        path: 'profile',
        name: 'Profile',
        meta: {
          title: '档案管理',
          icon: '📋'
        },
        children: [
          {
            path: 'elderly',
            name: 'ElderlyProfile',
            component: () => import('../views/profile/elderly/index.vue'),
            meta: {
              title: '老人档案',
              icon: '👴'
            }
          },
          {
            path: 'elderly/:id',
            name: 'ElderlyDetail',
            component: () => import('../views/profile/elderly/detail.vue'),
            meta: {
              title: '老人详情',
              hideInMenu: true
            }
          },
          {
            path: 'family',
            name: 'FamilyProfile',
            component: () => import('../views/profile/family/index.vue'),
            meta: {
              title: '家属管理',
              icon: '👨‍👩‍👧‍👦'
            }
          },
          {
            path: 'relation',
            name: 'ProfileRelation',
            component: () => import('../views/profile/relation/index.vue'),
            meta: {
              title: '关系管理',
              icon: '🔗'
            }
          },
          {
            path: 'family/:id',
            name: 'FamilyDetail',
            component: () => import('../views/profile/family/detail.vue'),
            meta: {
              title: '家属详情',
              hideInMenu: true
            }
          }
        ]
      },

      // 健康管理
      {
        path: 'health',
        name: 'Health',
        meta: {
          title: '健康管理',
          icon: '❤️'
        },
        children: [
          {
            path: 'data',
            name: 'HealthData',
            component: () => import('../views/health/data/index.vue'),
            meta: {
              title: '健康数据',
              icon: '📊'
            }
          },
          // {
          //   path: 'monitor',
          //   name: 'HealthMonitor',
          //   component: () => import('../views/health/data/monitor.vue'),
          //   meta: {
          //     title: '实时监控',
          //     icon: '📈'
          //   }
          // },
          {
            path: 'warning',
            name: 'HealthWarning',
            component: () => import('../views/health/warning/index.vue'),
            meta: {
              title: '预警管理',
              icon: '⚠️'
            }
          },
          {
            path: 'warning/rules',
            name: 'WarningRules',
            component: () => import('../views/health/warning/rules.vue'),
            meta: {
              title: '预警规则',
              icon: '📝'
            }
          }
          // {
          //   path: 'plan',
          //   name: 'HealthPlan',
          //   component: () => import('../views/health/plan/index.vue'),
          //   meta: {
          //     title: '健康方案',
          //     icon: '📋'
          //   }
          // },
          // {
          //   path: 'plan/template',
          //   name: 'PlanTemplate',
          //   component: () => import('../views/health/plan/template.vue'),
          //   meta: {
          //     title: '方案模板',
          //     icon: '📄'
          //   }
          // }
        ]
      },

      // 药品管理
      {
        path: 'medicine',
        name: 'Medicine',
        meta: {
          title: '药品管理',
          icon: '💊'
        },
        children: [
          {
            path: 'info',
            name: 'MedicineInfo',
            component: () => import('../views/medicine/info/index1.vue'),
            meta: {
              title: '药品信息',
              icon: '💉'
            }
          },
          // {
          //   path: 'record',
          //   name: 'MedicineRecord',
          //   component: () => import('../views/medicine/record/index.vue'),
          //   meta: {
          //     title: '用药记录',
          //     icon: '📝'
          //   }
          // },
          // {
          //   path: 'reminder',
          //   name: 'MedicineReminder',
          //   component: () => import('../views/medicine/reminder/index.vue'),
          //   meta: {
          //     title: '用药提醒',
          //     icon: '⏰'
          //   }
          // }
        ]
      },

      // 预约管理
      {
        path: 'appointment',
        name: 'Appointment',
        meta: {
          title: '预约管理',
          icon: '📅'
        },
        children: [
          {
            path: 'visit',
            name: 'VisitAppointment',
            component: () => import('../views/appointment/visit/index.vue'),
            meta: {
              title: '探访预约',
              icon: '🏠'
            }
          },
          {
            path: 'tour',
            name: 'TourAppointment',
            component: () => import('../views/appointment/tour/index.vue'),
            meta: {
              title: '参观预约',
              icon: '🚶'
            }
          }
        ]
      },

      // 访客管理
      {
        path: 'visitor',
        name: 'Visitor',
        meta: {
          title: '在住管理',
          icon: '🚪'
        },
        children: [
          // {
          //   path: 'record',
          //   name: 'VisitorRecord',
          //   component: () => import('../views/visitor/record/index.vue'),
          //   meta: {
          //     title: '来访记录',
          //     icon: '📝'
          //   }
          // },
          // {
          //   path: 'admission',
          //   name: 'VisitorAdmission',
          //   component: () => import('../views/visitor/admission/index.vue'),
          //   meta: {
          //     title: '入住管理',
          //     icon: '🏠'
          //   }
          // },
          {
            path: 'admission/application',
            name: 'AdmissionApplication',
            component: () => import('../views/visitor/admission/application.vue'),
            meta: {
              title: '在住管理',
              icon: '📋'
            }
          }
          // {
          //   path: 'discharge',
          //   name: 'VisitorDischarge',
          //   component: () => import('../views/visitor/discharge/index.vue'),
          //   meta: {
          //     title: '退住管理',
          //     icon: '🚪'
          //   }
          // }
        ]
      },

      // 住宿管理
      {
        path: 'residence',
        name: 'Residence',
        meta: {
          title: '住宿管理',
          icon: '🏨'
        },
        children: [
          {
            path: 'bed',
            name: 'ResidenceBed',
            component: () => import('../views/residence/bed/index.vue'),
            meta: {
              title: '床位管理',
              icon: '🛏️'
            }
          },
          {
            path: 'leave',
            name: 'ResidenceLeave',
            component: () => import('../views/residence/leave/index.vue'),
            meta: {
              title: '请假管理',
              icon: '📅'
            }
          }
        ]
      },

      // 财务管理
      {
        path: 'finance',
        name: 'Finance',
        meta: {
          title: '财务管理',
          icon: '💰'
        },
        children: [
          {
            path: 'bill',
            name: 'FinanceBill',
            component: () => import('../views/finance/bill/index.vue'),
            meta: {
              title: '账单管理',
              icon: '📄'
            }
          },
          {
            path: 'refund',
            name: 'FinanceRefund',
            component: () => import('../views/finance/refund/index.vue'),
            meta: {
              title: '退款管理',
              icon: '🔄'
            }
          }
        ]
      },

      // 订单管理
      {
        path: 'order',
        name: 'Order',
        meta: {
          title: '订单管理',
          icon: '📦'
        },
        children: [
          {
            path: 'feedback',
            name: 'OrderFeedback',
            component: () => import('../views/order/feedback/index.vue'),
            meta: {
              title: '反馈管理',
              icon: '💬'
            }
          }
        ]
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 智慧养老管理平台`
  }
  if(to.path === '/login' ) return next();
  //这里可以添加权限验证逻辑
  const token = localStorage.getItem('token')
  if (!token) {
    ElMessage.error('请先登录')
    next('/login')
  } else {
    next()
  }
  
  next()
})

export default router
// 这里可以添加全局的路由守卫逻辑，例如权限验证、日志